<template>
  <div class="UploadExcel">
    <UploadExcel :on-success="UploadSuccess" />
  </div>
</template>

<script>
import { importEmployees } from '@/api/employees'
export default {
  methods: {
    async UploadSuccess(obj) {
      console.log(obj.header)
      console.log(obj.results)

      const data = obj.results.map(item => {
        const user = {}
        user.username = item['姓名']
        user.mobile = item['手机号']
        user.workNumber = item['工号']
        user.formOfEmployment = +item['聘用形式']
        user.timeOfEntry = new Date(this.formatExcelTime(item['入职日期']))
        user.corretionTime = new Date(this.formatExcelTime(item['转正日期']))
        console.log(user)
        return user
      })
      const res = await importEmployees(data)
      console.log(res)
    },
    // 专门转换 excel 日期数据
    formatExcelTime(num, format = '-') {
      num = Number(num)	// 强制类型转化，以防传来的值是字符串
      let millisecond = 0	// 转化后的毫秒数
      if (num > 60) {
        millisecond = (num - 25569) * 60 * 60 * 24 * 1000
      } else {
        millisecond = (num - 25568) * 60 * 60 * 24 * 1000
      }
      const date = new Date(millisecond)	// 根据转化后的毫秒数获取对应的时间
      const yy = date.getFullYear()
      const mm = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1
      const dd = date.getDate() < 10 ? '0' + date.getDate() : date.getDate()
      return yy + format + mm + format + dd	// 返回格式化后的日期
    }
  }
}
</script>

<style scoped>
.UploadExcel{
  height: 80vh;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
